<?php

/**
 * This is the model class for table "zy_gift_password".
 *
 * The followings are the available columns in table 'zy_gift_password':
 * @property integer $gift_password_id
 * @property integer $activity_id
 * @property integer $user_id
 * @property string $password
 * @property integer $state
 * @property string $time
 * @property string $service_number
 * @property string $bak2
 * @property integer $is_delete
 * @property string $crate_time
 * @property string $crate_user
 * @property string $update_time
 * @property string $update_user
 */
class GiftPassword extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return GiftPassword the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'zy_gift_password';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('password', 'required'),
			array('suser_id, state, is_delete,bak4', 'numerical', 'integerOnly'=>true),
			array('activity_id, crate_user, update_user', 'length', 'max'=>50),
			array('time', 'length', 'max'=>24),
			array('service_number, bak2', 'length', 'max'=>100),
			array('crate_time, update_time', 'safe'),
			array('address,bak3,bak5','length', 'max'=>300),
			array('moblie','length', 'max'=>14),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('gift_password_id, activity_id, suser_id, password, state, time, service_number, bak2,moblie,address,bak3,bak4,bak5, is_delete, crate_time, crate_user, update_time, update_user', 'safe','on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				 'activity'=>array(self::BELONGS_TO, 'Activity', 'activity_id,activity_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'gift_password_id' => Yii::t('base','Gift Password Id'),
			'activity_id' => Yii::t('base','Title'),
			'suser_id' => 'User',
			'password' => Yii::t('base','Gift Password'),
			'state' => Yii::t('base','Status'),
			'time' => Yii::t('base','Expiry Date'),
			'service_number' => Yii::t('base','Service Number'),
			'bak2' => 'Bak2',
			'moblie' => Yii::t('base','Mobile'),
			'address' => Yii::t('base','Specific address'),
			'bak4' => 'Bak4',
			'bak3'=>'Bak3',
			'bak5'=>'Bak5',			
			'is_delete' => 'Is Delete',
			'crate_time' => 'Crate Time',
			'crate_user' => 'Crate User',
			'update_time' => 'Update Time',
			'update_user' => 'Update User',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter 

conditions.
	 * @return CActiveDataProvider the data provider that can return the 

models based on the search/filter conditions.
	 */
	
	//查询兑奖密码
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		
	//	$model=new Activity;
		$criteria=new CDbCriteria;

		$criteria->compare('gift_password_id',$this->gift_password_id);
		$criteria->compare('activity_id',$this->activity_id);
		//echo "activity-----".$this->activity_id;
		if(Yii::app()->user->level() != 1){
		   $criteria->compare('user_id',Yii::app()->user->id);
			$criteria->with = 'activity';
			$criteria->compare('state','0');
		}
		//$criteria->addInCondition('state',$results);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('state',$this->state,true);
		$criteria->compare('time',$this->time,true);
		$criteria->compare('moblie',$this->moblie,true);
		$criteria->compare('service_number',$this->service_number,true);
		$criteria->compare('bak2',$this->bak2,true);
		$criteria->compare('is_delete',$this->is_delete);
		$criteria->compare('crate_time',$this->crate_time,true);
		$criteria->compare('crate_user',$this->crate_user,true);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('update_user',$this->update_user,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	//获取兑奖记录
	public function search2()
	{
		
		$criteria=new CDbCriteria;
		$criteria->compare('gift_password_id',$this->gift_password_id);
		$criteria->compare('activity_id',$this->activity_id);
		if(Yii::app()->user->level() != 1){
			$criteria->compare('user_id',Yii::app()->user->id);
			$criteria->with = 'activity';
		}
		if($this->state==1||$this->state==2||$this->state==3)
		{
	
		$criteria->compare('state',$this->state);
		}
		$criteria->compare('password',$this->password,true);
		$criteria->compare('moblie',$this->moblie,true);
		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
				));
	}

	/**
	 * 获取活动名称
	 */
	 function getActivityName($num){
	 	if($num == ''){
	 		if(Yii::app()->user->level() != 1){
	 		$sql="select * from zy_activity where 

user_id='".Yii::app()->user->userParentId()."'";
	 		}else{
	 			$sql="select * from zy_activity";
	 		}
		 	$rs = yii::app()->db->createCommand($sql)->queryAll();
			return CHtml::listData($rs,'activity_id','title');
	 	}else{
	 		$sql="select * from zy_activity where user_id='".Yii::app()->user->userParentId()."' and activity_code='".$num."'";
		 	$rs = yii::app()->db->createCommand($sql)->queryAll();
		 	return $rs[0]['title'];
	 	}
	 }
	 
	 	/**
	 * 获取活动名称
	 */
	 function getActivityNames($num){
	 		$sql="select * from zy_activity where activity_id='".$num."'";
		 	$rs = yii::app()->db->createCommand($sql)->queryAll();
		 	return $rs[0]['title'];
	 }
	
	/**
	 * 兑奖状态
	 */
	 function getState($state){
	 	/*
		 	0. 没有分配给用户 (参与人数太少，少于奖品数

量  )
			1. 已经分配给用户，未领奖，未过期
			2. 已经分配给用户，未领奖，已过期
			3. 已经分配给客户，已领奖
		*/
	 	if($state == 0){
	 		return Yii::t

('base','Unassigned,Unclaimed,Not Expired');
	 	}elseif($state == 1){
	 		return Yii::t('base','Winner,Unclaimed,Not Expired');
	 	}elseif($state == 2){
	 		return Yii::t('base','Winner,Unclaimed,Expired');
	 	}elseif($state == 3){
	 		return Yii::t('base','Winner,To accept the award');
	 	}elseif($state == 4){
	 		return Yii::t('base','Unassigned,Unclaimed,Expired');
	 	}
	 }
	 
	 //获取地址
	 function getAddress($addressid){
	 	
	 	$sql="select * from zy_award_address where 

address_id='".$addressid."'";
	 	
	 	$rs = yii::app()->db->createCommand($sql)->queryAll();
	 	
	 	return $rs[0]['area'];
	 }
	 
	 
}